package com.viettel.data;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.viettel.BO.PortalUserBean;

public class LoginFetcher {

	private final String NAMESPACE = "http://webservice.viettel.com/";
	private final String METHOD_NAME = "actionLogin";
	private final String SOAP_ACTION = "http://webservice.viettel.com/actionLogin";
	private final String URL = "http://192.168.5.101:8084/RMSPortal/RMSWebService?wsdl";

	private SoapSerializationEnvelope envelope;
	private ArrayList<PortalUserBean> listResult;

	public LoginFetcher(String email, String password) {
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		// Ten dang nhap
		PropertyInfo userNameProperty = new PropertyInfo();
		userNameProperty.setName("email");
		userNameProperty.setValue(email);
		userNameProperty.setType(String.class);
		request.addProperty(userNameProperty);

		// Mat khau
		PropertyInfo passwordProperty = new PropertyInfo();
		passwordProperty.setName("password");
		passwordProperty.setValue(password);
		passwordProperty.setType(Long.class);
		request.addProperty(passwordProperty);

		envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);
	}

	public PortalUserBean getPortalUser() {
		HttpTransportSE httpRequest = new HttpTransportSE(URL);
		PortalUserBean bean = new PortalUserBean();
		try {
			httpRequest.call(SOAP_ACTION, envelope);
			SoapObject response2 = (SoapObject) envelope.bodyIn;
			int iToltalCount = response2.getPropertyCount();
			if (iToltalCount > 0) {
				SoapObject element;
				element = (SoapObject) response2.getProperty(0);
				if (element.hasProperty("portalUserId")) {
					bean.setPortalUserId(Long.parseLong(element.getProperty(
							"portalUserId").toString()));
				}
				if (element.hasProperty("email")
						&& element.getProperty("email") != null) {
					bean.setEmail(element.getProperty("email").toString());
				}
				if (element.hasProperty("fullName")
						&& element.getProperty("fullName") != null) {
					bean.setFullName(element.getProperty("fullName").toString());
				}
				if (element.hasProperty("dateOfBirth")
						&& element.getProperty("dateOfBirth") != null) {
					bean.setDateOfBirth(element.getProperty("dateOfBirth")
							.toString());
				}
				if (element.hasProperty("sex")
						&& element.getProperty("sex") != null) {
					bean.setSex(element.getProperty("sex").toString());
				}
				if (element.hasProperty("country")
						&& element.getProperty("country") != null) {
					bean.setCountry(element.getProperty("country").toString());
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return bean;
	}
}
